package com.jp.knowledge.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.activity.JpMainActivity;
import com.jp.knowledge.view.TeamCircleTopView;

/**
 * Created by 528787067@qq.com on 2017/4/18.
 */

public class TeamCircleMainFragment extends BaseFragment implements View.OnClickListener {

    private Context mContext;
    private View contentView;

    private TeamCircleTopView topView;
    private ImageView topRightBtn;

    private FragmentManager fragmentManager;
    private TeamCircleFragment teamCircleFragment;
    private TeamCircleLibraryFragment teamCircleLibraryFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        contentView = View.inflate(mContext, R.layout.fragment_team_circle_main, null);

        teamCircleFragment = new TeamCircleFragment();
        teamCircleLibraryFragment = new TeamCircleLibraryFragment();

        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.team_circle_content, teamCircleFragment)
                .add(R.id.team_circle_content, teamCircleLibraryFragment)
                .hide(teamCircleLibraryFragment).show(teamCircleFragment)
                .commit();

        initTitleBar();

        return contentView;
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        teamCircleFragment.setTitleBarVisibility(false);
    }

    private void initTitleBar(){
        View topBar = contentView.findViewById(R.id.top_bar);
        topBar.findViewById(R.id.module_name).setVisibility(View.GONE);
        topView = new TeamCircleTopView(mContext);
        topRightBtn = (ImageView) topBar.findViewById(R.id.icon_right);
        topRightBtn.setVisibility(View.VISIBLE);
        topRightBtn.setImageResource(R.mipmap.edit);
        topRightBtn.setOnClickListener(this);
        topBar.findViewById(R.id.icon_serach).setVisibility(View.GONE);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            int statusBarHeight = 0;
//            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
//            if (resourceId > 0)
//                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
//            topBar.setPadding(0, statusBarHeight, 0, 0);
//        }
        ((RelativeLayout)topBar.findViewById(R.id.top_bar_main_view)).addView(topView);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) topView.getLayoutParams();
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        topView.setOnItemSelectedListener(new TeamCircleTopView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if(index == 0) {
                    topRightBtn.setImageResource(R.mipmap.edit);
                    fragmentManager.beginTransaction().hide(teamCircleLibraryFragment)
                            .show(teamCircleFragment).commit();
                } else {
                    topRightBtn.setImageResource(R.mipmap.guanzhutianjia);
                    fragmentManager.beginTransaction().hide(teamCircleFragment)
                            .show(teamCircleLibraryFragment).commit();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v != topRightBtn)
            return;
        if(topView.getSelectedItemIndex() == 0){            // 动态
            teamCircleFragment.onClick(v);
        } else if(topView.getSelectedItemIndex() == 1){     // 文库

        }
    }
}
